#include "editinfo.h"
#include "ui_editinfo.h"

editinfo::editinfo(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::editinfo)
{
    ui->setupUi(this);
    ui->lineEdit_4->setEchoMode(QLineEdit::Password);;//密文输入
    ui->lineEdit_5->setEchoMode(QLineEdit::Password);;//密文
}

editinfo::~editinfo()
{
    delete ui;
}

void editinfo::setuser(QString username){
    user=username;
}

//个人信息提交
void editinfo::on_pushButton_2_clicked()
{
    QString name;
    QString phone;
    QString email;
    name=ui->lineEdit->text();
    phone=ui->lineEdit_2->text();
    email=ui->lineEdit_3->text();
    if(name.isEmpty()||phone.isEmpty()||email.isEmpty()){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("输入的信息不能为空"), QMessageBox::Yes);
    }
    else{
        ui->lineEdit->clear();
        ui->lineEdit_2->clear();
        ui->lineEdit_3->clear();

        QSqlQuery query;
        QString str = QString("update userinfo set name='%1',phone='%2',email='%3' where username='%4';").arg(name).arg(phone).arg(email).arg(this->user);
        if(query.exec(str)){
            QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("修改成功"), QMessageBox::Yes);
            this->close();//关闭返回用户界面;
            //发送刷新信号 为了刷新用户界面的显示
            emit(send_edit_flush());
        }
        else
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("删除失败"), QMessageBox::Yes);
    }
}
//取消
void editinfo::on_pushButton_4_clicked()
{
    this->close();
}

void editinfo::on_pushButton_3_clicked()
{
    this->close();
}
//修改密码
void editinfo::on_pushButton_clicked()
{
    QString password1=ui->lineEdit_4->text();
    QString password2=ui->lineEdit_5->text();
    if(password1.isEmpty()||password2.isEmpty()){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("输入的信息不能为空"), QMessageBox::Yes);
    }
    else if(password1!=password2){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("两次密码不一致"), QMessageBox::Yes);
    }
    else{
        ui->lineEdit_4->clear();
        ui->lineEdit_5->clear();
        QSqlQuery query;
        QString str = QString("update userinfo set password='%1' where username='%2';").arg(password2).arg(this->user);
        if(query.exec(str)){
            QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("修改成功"), QMessageBox::Yes);
            this->close();//关闭返回用户界面;
            //发送刷新信号 为了刷新用户界面的显示
            emit(send_edit_flush());
        }
        else
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("修改失败"), QMessageBox::Yes);
    }

}
